<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryRef" inline v-show="showSearch">
          <el-form-item label="知识点名称" prop="knowledge">
            <el-input
              v-model="queryParams.knowledge"
              placeholder="请输入知识点名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学科名称" prop="subId">
            <el-select
              v-model="queryParams.subId"
              placeholder="请选择"
              @change="handleQuery"
            >
              <el-option
                v-for="item in subjectOptions"
                :key="item.id"
                :label="item.subject"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <div class="table-tools mb8">
          <div>
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              >新增</el-button
            >
            <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              >修改</el-button
            >
          </div>
          <div>
            <right-toolbar
              v-model:showSearch="showSearch"
              @queryTable="getList"
            />
          </div>
        </div>

        <el-table
          v-loading="loading"
          :data="knowledgeList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="知识点名称"
            align="center"
            key="knowledge"
            prop="knowledge"
            show-overflow-tooltip
          />
          <el-table-column
            label="所属学科"
            align="center"
            key="subject"
            prop="subject"
            show-overflow-tooltip
          />
          <!-- <el-table-column
            label="创建时间"
            align="center"
            prop="creatTime"
            width="160"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.creatTime) }}</span>
            </template>
          </el-table-column> -->
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                type="primary"
                link
                icon="Edit"
                @click="handleUpdate(scope.row)"
                >修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改知识点配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        :model="form"
        :rules="rules"
        ref="knowledgeRef"
        label-width="120px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="学科名称" prop="subId">
              <el-select
                v-model="form.subId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in subjectOptions"
                  :key="item.id"
                  :label="item.subject"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="知识点名称" prop="knowledge">
              <el-input
                v-model="form.knowledge"
                placeholder="请输入知识点名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="knowledge">
import { getSubjectList } from "@/api/core/subject";
import {
  getTopicList,
  deleteTopicIds,
  addTopic,
  editTopic,
} from "@/api/core/topic";
import {
  getCurrentInstance,
  reactive,
  ref,
  toRefs,
  watch,
  computed,
} from "vue";
import { parseTime } from "@/utils/ruoyi";
import useUserStore from "@/store/modules/user";

const { proxy } = getCurrentInstance();

const knowledgeList = ref([]);
const subjectOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// const dateRange = ref([]);
const stuFlag = ref("");
const userStore = useUserStore();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    knowledge: undefined,
    subId: undefined,
  },
  currentRow: {},
  rules: {
    knowledge: [
      { required: true, trigger: "blur", message: "知识点名称不能为空" },
    ],
    subId: [
      {
        required: true,
        trigger: "blur",
        message: "知识点不能为空",
      },
    ],
  },
});
const { queryParams, form, rules, currentRow } = toRefs(data);

/** 查询知识点列表 */
function getList() {
  loading.value = true;
  // proxy.addDateRange(queryParams.value, dateRange.value)
  getTopicList(queryParams.value).then((res) => {
    loading.value = false;
    knowledgeList.value = res.rows;
    total.value = res.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  // dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const classIds = row.id || ids.value.toString();
  proxy.$modal
    .confirm("是否确认删除知识点？")
    .then(function () {
      return deleteTopicIds(classIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 重置操作表单 */
function reset() {
  form.value = {
    id: null,
    knowledge: "",
    subId: "",
  };
  proxy.resetForm("knowledgeRef");
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加知识点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value.id = row.id;
  form.value.knowledge = row.knowledge;
  form.value.subId = row.subId;
  open.value = true;
  title.value = "修改知识点";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["knowledgeRef"].validate((valid) => {
    if (valid) {
      if (form.value.id) {
        editTopic(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTopic(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
// 首次请求知识点列表
getSubjectList().then((res) => {
  subjectOptions.value = res.rows;
  getList();
});
</script>

<style scoped lang="scss"></style>
